#ifndef PROFILE_H
#define PROFILE_H

#include <QDialog>
#include <QVariantMap>
#include <../fields/databasefield.h>
#include <QSqlQuery>
#include <QLabel>
#include <QGridLayout>

namespace Ui {
class Profile;
}

typedef struct {
    QVector<QWidget *> delegates;
    QGridLayout *lay;
    QWidget *widget;
    QString selectQuery; // SELECT `field`,..,`fieldN`
    QString updateQuery; // UPDATE `people` SET `field`='%1',...,`fieldN`='%N'
} people_type_t;

typedef struct { // only for load config
    int updateIdx;
    int gridRow;
    int gridCol;
} people_type2_t;

class Profile : public QDialog
{
    Q_OBJECT
    
public:
    explicit Profile(QWidget *parent = 0);
    ~Profile();

    void loadProfile(int id);

private slots:
    void writeProfile();

private:
    void loadConfig();
    QHash<QString, int> loadPlugins(); // return: name of plugin - it's id, used later for m_fieldInterfaces
    void loadIdTypeMap();

    int m_last_type; // last people type
    int m_last_id; // last loaded id
    Ui::Profile *ui;
    QSqlQuery sql;
    QHash<int, int> m_peopleIdType; // id - type
    QHash<int, people_type_t> m_peopleTypes; // layout and delegates for each type of people
    QHash<int, DatabaseField *> m_fieldInterfaces; // loaded plugin interfaces by id
    QHash<QString, QWidget*> m_jsonIdDelegate; // json id - delegate

    QVariantMap m_conf; // profile.json
};

#endif // PROFILE_H
